package com.zikao.article.controller;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.zikao.article.entity.ArticleEntity;
import com.zikao.article.entity.TextEntity;
import com.zikao.article.service.ArticleService;
import com.zikao.article.service.TextService;
import com.zikao.common.service.RedisService;
import com.zikao.common.utils.MyMessage;
import com.zikao.user.entity.UserEntity;
import com.zikao.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.article.entity.RemarkEntity;
import com.zikao.article.service.RemarkService;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-02-21 10:55:38
 */
@Controller
@Slf4j
@RequestMapping("article/remark")
public class RemarkController {
    @Autowired
    private RemarkService remarkService;
    @Autowired
    private TextService textService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = remarkService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id){
		RemarkEntity remark = remarkService.getById(id);

        return R.ok().put("remark", remark);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody RemarkEntity remark, HttpServletRequest request, HttpServletResponse response){
        TextEntity textEntity = new TextEntity();
        try {
            textEntity.setContent(remark.getContent());
            textService.save(textEntity);
            remark.setTextId(textEntity.getId());
            remarkService.save(remark);

            // 写入redis,未读消息通知
            // 创建消息
            MyMessage message = new MyMessage();
            UserEntity sender = (UserEntity) request.getSession().getAttribute("user");
            message.setSenderId(sender.getId());
            message.setSenderName(sender.getName());
            ArticleEntity article = articleService.getArticleById(remark.getArticleId());
            UserEntity receiver = userService.getById(article.getUserId());
            message.setReceiverId(receiver.getId());
            message.setReceiverName(receiver.getName());
            message.setType(1);
            message.setUuid(UUID.randomUUID().toString());
            message.setCreateTime(new Date());
            message.setReceiverEmail(receiver.getEmail());
            message.setContent("用户 <a>"+sender.getName()+"</a> 对你发布的文章 <a href='javascript:;' onclick='seeMessage(\""+message.getUuid()+"\","+article.getId()+")'>"+article.getTitle()+"</a> 进行了评论");
            redisService.sendMessage(message);
        }catch (Exception e){
            log.error("程序发生错误-save:",e);
            return R.error();
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody RemarkEntity remark){
		remarkService.updateById(remark);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids){
		remarkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
